<?php

namespace Common\Model;
use think\Model;

class ImgModel extends BaseModel{

	//追加属性
    public $append = [
        'spec_num',         //规格数量
        'number',           //库存
        'show_number',      //库存
        'p_name',           //父节点名称
        'pics',             //图片数组
	];
    function getPNameAttr($value, &$data){
        if($data['pid']){
            $model = M('Imgcate');
            $wdata['id'] = $data['pid'];
            $pname = $model->where($wdata)->getField('title');
            return $pname;
        }
    }
  

    //搜索
    function _search(){
        $map = [];
        $status=I('status');
        $is_ls=I('is_ls');
        if(I('keyword')){
            $map['title|goods_sn'] = array('like','%'.I('keyword').'%');
        }
        $cat_id=I('pid');
        $map['status'] = $status;
        if($is_ls){
            $map['is_ls'] = $is_ls;
        }
        if($cat_id){
            $dra[]=$cat_id;
            //查询子分类
            $i_model = M('Imgcate');
            $spec_list = $i_model->field('id')->where(['pid'=>$cat_id])->select();
            if($spec_list){
              foreach($spec_list as $k=>$val){
                 $dra[]=$val['id'];
                 //查询三级分类
                 $cat_id2=$val['id'];
                 $spec_list2 = $i_model->field('id')->where(['pid'=>$cat_id2])->select();
                 if($spec_list2){
                    foreach($spec_list2 as $k1=>$vals){
                        $dra[]=$vals['id'];
                    }
                 }
              }
            }
            $map['pid']=['in',$dra];
        }
        $map['is_delete'] = I('is_delete') ? : 0;
        return $map;
    }

}